package com.ruoyi.safe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.enumeration.CommonDelFlag;
import com.ruoyi.safe.domain.TTopic;
import com.ruoyi.safe.mapper.TTopicMapper;
import com.ruoyi.safe.service.ITTopicService;
import com.ruoyi.safe.vo.topic.TopicItemVO;
import com.ruoyi.safe.vo.topic.TopicKeywordAppDTO;
import com.ruoyi.safe.vo.topic.TopicPageReqDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 话题管理Service业务层处理
 *
 * @author ruoyi
 * &#064;date  2025-03-31
 */
@Service
@Slf4j
public class TTopicServiceImpl implements ITTopicService {
    @Autowired
    private TTopicMapper tTopicMapper;

    /**
     * 查询话题管理
     *
     * @param id 话题管理主键
     * @return 话题管理
     */
    @Override
    public TTopic selectTTopicById(Long id) {
        return tTopicMapper.selectTTopicById(id);
    }

    /**
     * 查询话题管理列表
     *
     * @param tTopic 话题管理
     * @return 话题管理
     */
    @Override
    public List<TTopic> selectTTopicList(TopicPageReqDTO tTopic) {
        handlingNullValues(tTopic);
        log.debug("tTopic: {}", tTopic);
        LocalDateTime start = null;
        LocalDateTime end = null;
        if (tTopic.getCreateTime() != null) {
            start = tTopic.getCreateTime()[0];
            end = tTopic.getCreateTime()[1];
        }

        List<TTopic> tTopics = tTopicMapper.selectTTopicList(tTopic, start, end);
        log.debug("Query result: {}", tTopics);
        return tTopics;
    }

    private static void handlingNullValues(TopicPageReqDTO tTopic) {
        LocalDateTime[] createTime = tTopic.getCreateTime();
        if (createTime != null) {
            if (createTime[0] == null) {
                tTopic.setCreateTime(new LocalDateTime[]{LocalDateTime.MIN, createTime[1]});
            }
            if (createTime[1] == null) {
                tTopic.setCreateTime(new LocalDateTime[]{createTime[0], LocalDateTime.MAX});
            }
        }


        Integer[] postCount = tTopic.getPostCount();
        if (postCount == null) {
            postCount = new Integer[]{0, Integer.MAX_VALUE};
            tTopic.setPostCount(postCount);
        }
        if (postCount[0] == null) {
            tTopic.setPostCount(new Integer[]{0, postCount[1]});
        }
        if (postCount[1] == null) {
            tTopic.setPostCount(new Integer[]{postCount[0], Integer.MAX_VALUE});
        }

        Integer[] heatDeg = tTopic.getHeatDeg();
        if (heatDeg == null) {
            heatDeg = new Integer[]{0, Integer.MAX_VALUE};
            tTopic.setHeatDeg(heatDeg);
        }
        if (heatDeg[0] == null) {
            tTopic.setHeatDeg(new Integer[]{0, heatDeg[1]});
        }
        if (heatDeg[1] == null) {
            tTopic.setHeatDeg(new Integer[]{heatDeg[0], Integer.MAX_VALUE});
        }

    }

    /**
     * 新增话题管理
     *
     * @param tTopic 话题管理
     * @return 结果
     */

    @Override
    public int insertTTopic(TTopic tTopic) {
        tTopic.setCreateTime(DateUtils.getNowDate());
        return tTopicMapper.insertTTopic(tTopic);
    }

    /**
     * 修改话题管理
     *
     * @param tTopic 话题管理
     * @return 结果
     */
    @Override
    public int updateTTopic(TTopic tTopic) {
        tTopic.setUpdateTime(DateUtils.getNowDate());
        return tTopicMapper.updateTTopic(tTopic);
    }

    /**
     * 批量删除话题管理
     *
     * @param ids 需要删除的话题管理主键
     * @return 结果
     */
    @Override
    public int deleteTTopicByIds(Long[] ids) {
        return tTopicMapper.deleteTTopicByIds(ids);
    }

    /**
     * 删除话题管理信息
     *
     * @param id 话题管理主键
     * @return 结果
     */
    @Override
    public int deleteTTopicById(Long id) {
        return tTopicMapper.deleteTTopicById(id);
    }

    @Override
    public List<TopicItemVO> hotThree() {
        List<TTopic> tTopics = tTopicMapper.hotThree();
        List<TopicItemVO> res = new ArrayList<>();
        tTopics.forEach(tTopic -> res.add(new TopicItemVO(tTopic.getId(), tTopic.getTitle(), tTopic.getHeatDeg())));
        return res;
    }

    @Override
    public PageResult<TopicItemVO> selectByKeyword(TopicKeywordAppDTO dto) {
        String keyword = dto.getKeyword();
        Integer pageNo = dto.getPageNo();
        Integer pageSize = dto.getPageSize();
        if (keyword == null) {
            keyword = "";
        }
        IPage<TTopic> page = new Page<>(pageNo, pageSize);
        IPage<TTopic> tTopicIPage = tTopicMapper.selectPage(page, new LambdaQueryWrapper<TTopic>().like(TTopic::getTitle, keyword).eq(TTopic::getDelFlag, CommonDelFlag.NO_DEL.getCode()));
        List<TTopic> tTopics = page.getRecords();

        List<TopicItemVO> res = new ArrayList<>();
        tTopics.forEach(tTopic -> res.add(new TopicItemVO(tTopic.getId(), tTopic.getTitle(), tTopic.getHeatDeg())));
        return new PageResult<>(res, page.getTotal());
    }

    @Override
    public boolean updateStatus(Integer delFlag, Long topicId) {
        TTopic tTopic = tTopicMapper.selectById(topicId);
        if (tTopic == null) {
            throw new ServiceException("话题不存在");
        }
        if (delFlag != 1 && delFlag != 0) {
            throw new ServiceException("delFlag值错误");
        }
        tTopic.setDelFlag(delFlag);
        tTopicMapper.updateById(tTopic);
        return true;
    }
}
